package com.cnbot.dchttpsdk.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.cnbot.dchttpsdk.R;
import com.cnbot.dchttpsdk.helper.ProgressCancelListener;


/**
 * Copyright (c) 2016-a  Hunan Cnbot Co., Ltd. All Rights Reserved.
 *
 * @descriptoin 网络请求弹窗的具体实现类，只需要实现IProgressView接口实现其中方法即可随意扩展
 * @FileName: ProgressView.java
 * @author: dc
 * @date: 2019/1/19 14:13
 * @version: 1.0
 */

public class ProgressView implements IProgressView, DialogInterface.OnCancelListener {

    private ProgressDialog pd;
    private ProgressCancelListener mCancelListener;

    public ProgressView(Context context, boolean cancelable, String des) {
        pd = new ProgressDialog(context);
        pd.setCancelable(cancelable);
        pd.setCanceledOnTouchOutside(false);
        pd.setMessage(des);
    }

    public ProgressView(Context context, boolean cancelableOnOutside) {
        pd = new ProgressDialog(context);
        pd.setCancelable(true);
        pd.setCanceledOnTouchOutside(cancelableOnOutside);
        pd.setMessage(context.getString(R.string.progress_view_title));
    }

    public ProgressView(Context context) {
        pd = new ProgressDialog(context);
        pd.setCancelable(true);
        pd.setCanceledOnTouchOutside(false);
        pd.setMessage(context.getString(R.string.progress_view_title));
    }

    public ProgressView(Context context, ProgressCancelListener listener) {
        mCancelListener = listener;
        pd = new ProgressDialog(context);
        pd.setCancelable(true);
        pd.setCanceledOnTouchOutside(false);
        pd.setMessage(context.getString(R.string.progress_view_title));
        pd.setOnCancelListener(this);
    }

    public ProgressView(final Context context, ProgressViewHandler progressHandler, ProgressCancelListener listener) {
        mCancelListener = listener;
        progressHandler.setProgressView(this);
        progressHandler.post(new Runnable() {
            @Override
            public void run() {
                pd = new ProgressDialog(context);
                pd.setCancelable(true);
                pd.setCanceledOnTouchOutside(false);
                pd.setMessage(context.getString(R.string.progress_view_title));
                pd.setOnCancelListener(ProgressView.this);

            }
        });

    }

    @Override
    public IProgressView initProgressView(Context context) {

        return new ProgressView(context);
    }

    @Override
    public void showProgressView(String des) {
        if (pd != null && !pd.isShowing()) {
            pd.setMessage(des);
            pd.show();
        }
    }

    @Override
    public void showProgressView() {
        if (pd != null && !pd.isShowing()) {
            pd.show();
        }
    }

    @Override
    public void hideProgressView(long delay) {

    }

    @Override
    public void hideProgressView() {
        if (pd != null && pd.isShowing()) {
            pd.dismiss();
        }
    }

    @Override
    public boolean isShowing() {
        if (pd == null) {
            return false;
        }

        return pd.isShowing();
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (mCancelListener != null) {
            mCancelListener.onCancelProgress();
        }
    }
}